<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Push extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see https://codeigniter.com/user_guide/general/urls.html
	 */
	public function index()
	{
		$this->load->view('welcome_message');
	}
    
    public function notice(){
		
		//设置MIME为text/event-stream
        header('Content-Type: text/event-stream');

		$counter = rand(1, 10);
		
		while (1) {
		  // Every second, sent a "ping" event.
		  
		  echo "event: ping\n";
		  $curDate = date(DATE_ISO8601);
		  echo 'data: {"time": "' . $curDate . '"}';
		  echo "\n\n";
		  
		  // Send a simple message at random intervals.
		  
		  $counter--;
		  
		  if (!$counter) {
			echo 'data: This is a message at time ' . $curDate . "\n\n";
			$counter = rand(1, 10);
		  }
		  
		  
		  echo "event:push \n";
		  
		  echo 'data:{pushingtime:"'.$curDate.'"}';
		  
		  echo "\n\n";
		  
		  ob_flush();
		  flush();
		  sleep(1);
		}
    }
}
